<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            v-for="(item, index) in commentInfo.tags"
            :key="index"
            @click="changeTag(item, index)"
            href="javascript:;"
            :class="{ active: activeIndex === index }"
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a
        href="javascript:;"
        v-for="item in sortList"
        :key="item.name"
        :class="{ active: query.sortField === item.value }"
        @click="query.sortField = item.value;query.page=1"
        >{{ item.name }}</a
      >
    </div>

    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ filterName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>

            <i
              v-for="i in 5 - item.score"
              :key="i"
              class="iconfont icon-wjx02"
            ></i>
            <span class="attr">{{ filterSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片预览组件 -->
          <GoodsCommentImage v-if="item.pictures.length" :pictures='item.pictures'></GoodsCommentImage>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <XtxPagination :curr-page='query.page' :total='total' @current-change='currentChange'></XtxPagination>
  </div>
</template>

<script>
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product'
import { inject, reactive, ref, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'
export default {
  components: { GoodsCommentImage },
  name: 'GoodsComment',
  setup () {
    const commentInfo = ref({})
    const activeIndex = ref(0)
    const list = ref([])
    const goods = inject('goods')
    // 商品的评价信息
    findGoodsCommentInfo(goods.value.id).then(({ result }) => {
      // console.log(result)
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      commentInfo.value = result
      // goods 是响应式对象，修改的话可以更新到父组件组件上
      goods.value.commentCount = result.evaluateCount
    })

    // 获取评论列表接口的查询参数
    const query = reactive({
      page: 1, // 是 1 页码
      pageSize: 10, // 是 10 页尺寸
      hasPicture: null, // 否 true 是否有图
      tag: null, // 否 质量好 标签
      sortField: null, // 否 praiseCount 排序字段，可选值范围[praiseCount,createTime]
      sortMethod: null // 否 desc 排序方法，可选值范围[asc,desc],默认为desc
    })

    const total = ref(0)

    watch(
      query,
      () => {
        findGoodsCommentList(goods.value.id, query).then(({ result }) => {
          console.log(result)
          list.value = result.items
          total.value = result.counts
        })
      },
      { immediate: true }
    )

    // 替换名字变成 ***
    const filterName = (str) => {
      // console.log(str)
      return str.slice(0, 1) + '***' + str.slice(-1)
    }

    // 把商品规格数组，处理成字符串展示
    const filterSpecs = (arr) => {
      // console.log(arr)
      return arr.map((item) => `${item.name}:${item.nameValue}`).join(' ')
    }

    const changeTag = (theItem, index) => {
      // 高亮效果
      activeIndex.value = index

      console.log(theItem)
      if (theItem.title === '全部评价') {
        query.hasPicture = null
        query.tag = null
      } else if (theItem.title === '有图') {
        query.hasPicture = true
        query.tag = null
      } else {
        query.hasPicture = null
        query.tag = theItem.title
      }
      console.log(query)
      query.page = 1
    }

    //  <a href="javascript:;" class="active">默认</a>
    //   <a href="javascript:;">最新</a>
    //   <a href="javascript:;">最热</a>
    const sortList = [
      { name: '默认', value: null },
      { name: '最新', value: 'createTime' },
      { name: '最热', value: 'praiseCount' }
    ]

    const currentChange = (val) => {
      console.log(val)
      query.page = val
    }

    return {
      commentInfo,
      activeIndex,
      list,
      filterName,
      filterSpecs,
      changeTag,
      sortList,
      query,
      currentChange,
      total
    }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
